//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by rk on 15/10/6.
//  Copyright © 2015年 kyy. All rights reserved.
//

import UIKit

/*
   1.创建代理协议; 声明一个代理
   2.让代理遵守代理协议, 设置代理

*/

//创建代理协议
protocol VisitorLoginViewDelegate : NSObjectProtocol{

    //注册
    func visitorWillRegister()
    //登录
    func visitorWillLogin()


}

class VisitorLoginView: UIView {
    //声明一个代理, 为了防止循环引用,需要使用weak
    weak var visitorViewDelegate:VisitorLoginViewDelegate?
    

    @IBOutlet weak var smallIcon: UIImageView!
    
    @IBOutlet weak var iconView: UIImageView!

    @IBOutlet weak var tipLabel: UILabel!
    
    
    func setUIInfo(image_name imageName:String, tipText: String, isHome: Bool = false){
    
        iconView.image = UIImage(named: imageName)
        tipLabel.text = tipText
        smallIcon.hidden = !isHome
        if isHome {
            
            startAnimation()
        }
        
    
    }
    
    
//MARK: -----给smallIcon添加动画效果
    private func startAnimation(){
        //创建基础动画 旋转
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //动画重复次数
        animation.repeatCount = MAXFLOAT
        //转一圈需要的时间 duration:持续
        animation.duration = 10
        //转一圈
        animation.toValue = 2 * M_PI
        //当动画界面非活跃状态时, 不停止动画 (此属性默认值为YES,默认会移除动画图层)
        animation.removedOnCompletion = false
        //将动画添加给smallIcon
        smallIcon.layer.addAnimation(animation, forKey: nil)
    
    }
    
    @IBAction func registerAction(sender: AnyObject) {
        //OC 中 if(delegate response){}
        
        visitorViewDelegate?.visitorWillRegister()
        
    }
    
    
    @IBAction func loginAction(sender: AnyObject) {
        
        visitorViewDelegate?.visitorWillLogin()
        
    }
    
    
    
    
}
